fix: prevent back-button trap on partial redirects#3786
Closed
bartlomieju wants to merge 1 commit into
Closed
Conversation
When clicking a link that triggers a server redirect (e.g. /docs → /docs/syntax), both URLs were pushed to history, creating a back-button trap: pressing back would go to /docs, which immediately redirects back to /docs/syntax. Root cause: the click handler pushed the clicked URL before fetch, then fetchPartials pushed the redirect target after — two pushState calls for one navigation. Fix: pull history management out of fetchPartials (which now just returns the final URL) and let each caller handle history appropriately: - Click handler: pushState before fetch, replaceState after if redirected - Form handler: pushState after fetch with the final URL (preserves POST-redirect-GET) - Popstate handler: replaceState if redirected (prevents infinite loops on back/forward) - Button handler: no history changes Also fixes: - ?fresh-partial query param stripped from URLs before they enter history - updateLinks() now uses the post-redirect URL for correct active link highlighting - Form submissions now save scroll position before fetch (was previously missed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes back-button trap when client-side partial navigation encounters a server redirect (e.g.
/docs→/docs/data-modelling/syntax). Previously both URLs were pushed to history, so pressing back would navigate to/docswhich immediately redirects forward again — making it impossible to go back past the redirect.Root cause: The click handler called
maybeUpdateHistory(clickedUrl)before the fetch, thenfetchPartialscalledmaybeUpdateHistory(redirectedUrl)after — twopushStatecalls for a single navigation.Fix: Remove history management from
fetchPartials(it now returns the final URL) and let each caller handle history for its specific context:pushState(clicked URL)replaceState(swap to final URL)saveScrollPositiononlypushState(final URL)replaceState(prevents infinite loop)This also:
?fresh-partialfrom URLs before they enter history (was leaking as an implementation detail)updateLinks(), fixing active link highlighting after redirectssaveScrollPosition()before form fetches (was previously missing — scroll was saved after DOM change)Supersedes #3776.
Test plan
/docs→/docs/syntax) — only final URL should appear in history, back button returns to previous page?fresh-partialnever visible in address bar or history